<?php

namespace Home\Controller;
use Think\Controller;
class LogController extends HController {
    //展示日志列表
    public function index(){
        // 1 判断登录
        if (empty($_GET['user_id'])) {
            $vuid = $_SESSION['qzone']['home']['id'];
        } else {
            $vuid = $_GET['user_id'];//被访问用户id
        }
        $user_model = M('User');
        $vuser = $user_model->find($vuid);//查询被访问用户信息
        $model = M('log');
        $count = $model->where(array('uid'=>$vuid,'state'=>0))->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(20)
        $show = $Page->show();// 分页显示输出
        $list = $model->where(array('uid'=>$vuid,'state'=>0))->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('vuser',$vuser);// 赋值被访问用户信息输出
        $this->display();
    }

    //日志评论回复
    public function comhf(){
        if (IS_AJAX){
            $uid =$_SESSION['qzone']['home']['id'];
            $log_comment_huif['com_id'] = I('post.com_id');
            $log_comment_huif['content'] = I('post.content');
            $log_comment_huif['hf_id'] = I('post.hfid');
            $log_comment_huif['uid'] = $uid;
            $log_comment_huif['ctime'] = time();

            $model = M('log_comment_huif');
            $model->create();

            $res = $model->add($log_comment_huif);
            $id = $res;

            if($res){
                $user_model = M('user');
                $user1 = $user_model->find($uid);
                $user2 = $user_model->find($log_comment_huif['hf_id']);
                $data['zonedp1'] = $user1['zonedp'];
                $data['name1'] = $user1['nickname'];
                $data['name2'] = $user2['nickname'];
                $data['status'] = 'success';
                $data['msg'] = '发表成功！';
                $data['id'] = $res;
                $data['url1'] = U('/Home/Index/index',array('user_id'=>$uid));
                $data['url2'] = U('/Home/Index/index',array('user_id'=>$user2['id']));
                $data['content'] = $log_comment_huif['content'];
                $data['ctime'] = date('Y-m-d H:i:s',$log_comment_huif['ctime']);
                $this->ajaxReturn($data);
            } else {
                $data['status'] = 'failed';
                $data['msg'] = '发表失败，请重试！';
                $this->ajaxReturn($data);
            } 
        }else {
            $data['status'] = 'failed';
            $data['msg'] = '非法请求';
            $this->ajaxReturn($data);
        }
    }

    //日志评论
    public function comment(){
        if (IS_AJAX){
            $uid =$_SESSION['qzone']['home']['id'];
            $log_comment['who_id'] = I('post.who_id');
            $log_comment['content'] = I('post.content');
            $log_comment['uid'] = $uid;
            $log_comment['ctime'] = time();

            $model = M('log_comment');
            $model->create();

            $res = $model->add($log_comment);
            $id = $res;

            if($res){
                $user_model = M('user');
                $user = $user_model->find($uid);
                $data['zonedp'] = $user['zonedp'];
                $data['name'] = $user['nickname'];
                $data['uid'] = $user['id'];
                $data['status'] = 'success';
                $data['msg'] = '发表成功！';
                $data['id'] = $res;
                $data['url'] = U('/Home/Index/index',array('user_id'=>$uid));
                $data['content'] = $log_comment['content'];
                $data['ctime'] = date('Y-m-d H:i:s',$log_comment['ctime']);
                $this->ajaxReturn($data);
            } else {
                $data['status'] = 'failed';
                $data['msg'] = '发表失败，请重试！';
                $this->ajaxReturn($data);
            } 
        }else {
            $data['status'] = 'failed';
            $data['msg'] = '非法请求';
            $this->ajaxReturn($data);
        }
    }
    // 显示日志及其评论详情
    public function detail(){
        $id = I('get.logid');
        $uid = $_SESSION['qzone']['home']['id'];//当前登录用户id
        if (empty($_GET['user_id'])) {
            $vuid = $_SESSION['qzone']['home']['id'];
        } else {
            $vuid = $_GET['user_id'];//被访问用户id
        }
        $logmodel = M('log');
        $log = $logmodel->where(array('id'=>$id,'state'=>0))->find();
        if($vuid!=$log['uid']){
            $this->error('无效请求');
            exit;
        }
        $usermodel = M('user');
        $vuser = $usermodel->find($vuid);
        $user = $usermodel->find($uid);
        $model = M('log_comment');
        $hfmodel = M('log_comment_huif');
        //获取日志评论列表
        $com = $model->table('qzone_log_comment c, qzone_user u')->where('c.uid=u.id')->field('u.id as uid,u.zonedp as zonedp,u.nickname as nname,c.content as content,c.ctime as ctime,c.id as cid')->where(array('c.who_id'=>$id))->order('ctime asc')->select();
        //获取日志评论回复列表
        foreach ($com as $key => $value) {
            $hflist = $hfmodel->where(array('com_id'=>$value['cid']))->select();
            foreach ($hflist as $k => $val) {
                $user1 = $usermodel->find($val['uid']);
                $user2 = $usermodel->find($val['hfid']);
                $hflist[$k]['user1'] = $user1;
                $hflist[$k]['user2'] = $user2;
            }
            $com[$key]['hflist'] = $hflist;
        }
        
        $check_model = D('Log');
        //获取日志的评论数量
        $num = $check_model->getComNum($id);

        //获取日志的点赞数量
        $zan_num = $check_model->getGoodNum($id);

        $goodmodel = M('log_good');
        $data['log_id'] = $id;
        $data['uid'] = $_SESSION['qzone']['home']['id'];

        $is_fav = $goodmodel->where($data)->count();
        
        $this->assign(array('log'=>$log,'user'=>$user,'vuser'=>$vuser,'com'=>$com,'num'=>$num,'zan_num'=>$zan_num,'is_fav'=>$is_fav));
        $this->display();
    }

    //写日志
    public function add(){
        if (empty($_GET['user_id'])) {
            $vuid = $_SESSION['qzone']['home']['id'];
        } else {
            $vuid = $_GET['user_id'];//被访问用户id
        }
        $uid = $_SESSION['qzone']['home']['id'];
        if($vuid!=$uid){
            $this->error('没有操作权限');
            exit;
        }
        $user_model = M('User');
        $user = $user_model->find($uid);
        $this->assign('vuser',$user);// 赋值用户信息输出
        
        $this->display();
    }
    
    //编辑日志
    public function edit(){
        $id = I('get.logid');
        //判断日志作者
        $check_model = D('Log');
        $res = $check_model->check_auth($id,'Log');
        if(!$res){
            $this->error('没有编辑权限');
            exit;
        }

        $model = M('log');
        $log = $model->find($id);

        $uid = $_SESSION['qzone']['home']['id'];
        $user_model = M('User');
        $user = $user_model->find($uid);
        $this->assign('vuser',$user);// 赋值用户信息输出
        $this->assign('log',$log);
        $this->display();
    }

    //保存编辑日志
    public function tosave(){
        //1 判断用户是否登录 todo
        //2 接收数据
        //3 保存到数据库
        if (IS_POST){
            $data = I('post.');
            $data['uid'] = $_SESSION['qzone']['home']['id'];
            $data['ctime'] = time();

            $model = M('log');
            $model->create();

            if(!empty($data['id'])){
                //判断日志作者
                $check_model = D('Log');
                $res = $check_model->check_auth($data['id']);

                if(!$res){
                    $this->error('没有编辑权限',U('Log/index'));
                    exit;
                }
                unset($data['ctime']);
                $res = $model->save($data);
                $id = $data['id'];
            } else {
                $res = $model->add($data);
                $id = $res;
                //创建一条动态
                $DModel = M('Dynamic');
                $DData['uid'] = $_SESSION['qzone']['home']['id'];
                $DData['username'] = $_SESSION['qzone']['home']['username'];
                $DData['zonedp'] = $_SESSION['qzone']['home']['zonedp'];
                //日志的动态内容格式是标题加部分内容
                $DData['content'] = "<p><a href='".U('log/detail',array('logid'=>$id))."'>".I('post.title')."</a></p>".mb_substr($data['content'],0,15,'utf-8');
                $DData['is_show'] = 1;
                $DData['ctime'] = time();
                $DModel->create();
                $DModel->add($DData);
            }

            if($res){
                $this->success('保存成功',"/Home/Log/detail?logid=$id");
            } else {
                $this->error('保存失败');
            }
        }else{
            $this->error('非法请求');
        }

    }

    //删除日志
    public function del(){
        if(IS_AJAX){
            $id = I('post.id');
            $uid = $_SESSION['qzone']['home']['id'];
            //判断日志作者
            $check_model = D('Log');
            $res = $check_model->check_auth($id,'Log');

            if(!$res){
                $data['status'] = 'failed';
                $data['msg'] = '没有删除权限';
                $this->ajaxReturn($data);
                exit;
            }
            $model = M('log');
            $res1 = $model->delete($id);
            $commodel = M('log_comment');
            $comhfmodel = M('log_comment_huif');
            $comlist = $commodel->where(array('who_id'=>$id))->select();
            foreach ($comlist as $k => $val) {
                $comhfmodel->where(array('com_id'=>$val['id']))->delete();
            }
            $res2 = $commodel->where(array('who_id'=>$id))->delete();
            $data['status'] = 'success';
            $data['msg'] = '删除成功';
            $this->ajaxReturn($data);
        }else{
            $id = I('get.logid');
            $uid = $_SESSION['qzone']['home']['id'];
            //判断日志作者
            $check_model = D('Log');
            $res = $check_model->check_auth($id,'Log');

            if(!$res){
                $this->error('没有删除权限');
                exit;
            }

            $model = M('log');
            $res1 = $model->delete($id);

            $commodel = M('log_comment');
            $comhfmodel = M('log_comment_huif');
            $comlist = $commodel->where(array('who_id'=>$id))->select();
            foreach ($comlist as $k => $val) {
                $comhfmodel->where(array('com_id'=>$val['id']))->delete();
            }
            $res2 = $commodel->where(array('who_id'=>$id))->delete();

            $this->success('删除成功',"/Home/Log/index");
        }
        
    }

    //删除日志评论
    public function delcom(){
        if(IS_AJAX){
            $id = I('post.id');
            //判断日志评论作者
            $check_model = D('Log');
            $res = $check_model->com_auth($id);

            if(!$res){
                $data['status'] = 'failed';
                $data['msg'] = '没有删除权限';
                $this->ajaxReturn($data);
                exit;
            }
            
            $model = M('log_comment');
            $res = $model->delete($id);

            $reply_model = M('log_comment_huif');
            $repdel = $reply_model->where(array('com_id'=>$id))->delete();

            $data['status'] = 'success';
            $data['msg'] = '删除评论成功';
            $this->ajaxReturn($data);

        } else {
            echo '来过';
        }
    }

    //删除对评论的回复
    public function delhf(){
        if (IS_AJAX) {
            $id = I('post.id');
            //判断删除权限
            $check_model = D('Log');
            $res = $check_model->com_hf_auth($id);

            if(!$res){
                $data['status'] = 'failed';
                $data['msg'] = '没有删除权限';
                $this->ajaxReturn($data);
                exit;
            }

            $hf_model = M('log_comment_huif');
            $res = $hf_model->delete($id);
            if ($res) {
                $data['status'] = 'success';
                $data['msg'] = '删除成功';
                $this->ajaxReturn($data);
            } else {
                $data['status'] = 'failed';
                $data['msg'] = '删除失败';
                $this->ajaxReturn($data);
            }
        } else {
            $this->error('非法请求');
        }
    }

    //点赞的ajax实现
    //点赞和删除同时实现，并且返回了该条说说的点赞数量
    public function fav(){
        if (IS_AJAX) {
            $id = I('post.id');
            $uid = $_SESSION['qzone']['home']['id'];

            $data['uid'] = $uid;
            $data['log_id'] = $id;

            $model=M('log_good');
            $res = $model->where($data)->find();
            $num = $model->where(array('log_id'=>$id))->count();
            if($res){
                $rtn = $model->delete($res['id']);
                if ($rtn) {
                    $info['status'] = 'success';
                    $info['msg'] = '取消赞成功';
                    $info['btn'] = '赞(' . ($num-1) . ')';
                    $info['num'] = $num-1;
                    $this->ajaxReturn($info);
                } else {
                    $info['status'] = 'failed';
                    $info['msg'] = '取消失败';
                    $info['num'] = $num;
                    $this->ajaxReturn($info);
                }
            } else{
                $rtn = $model->add($data);
                if($rtn){
                    $info['status'] = 'success';
                    $info['msg'] = '点赞成功';
                    $info['btn'] = '取消赞';
                    $info['num'] = $num+1;
                    $this->ajaxReturn($info);
                } else{
                    $info['status'] = 'failed';
                    $info['msg'] = '点赞失败';
                    $info['num'] = $num;
                    $this->ajaxReturn($info);
                }
            }
        }
    }
}